/** 
 * IK 中文分词 版本 5.0 
 * 分词存入文件索引 
 * 
 */
package lucene_ik;

import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;


public class IKAnalyzerDemo {
	static String indexPath = "ikindex";

	public static void main(String[] args) {
		// Lucene Document的域名
		String fieldName = "text";
		// 检索内容
		String text = "浙江传媒学院也是一所学校";
		// 实例化IKAnalyzer分词器
		Analyzer analyzer = new IKAnalyzer();
		Directory dir = null;
		IndexWriter iwriter = null;
		
		
			// 建立内存索引对象
			
			try {
				dir = FSDirectory.open(new File(indexPath));
				
				// 配置IndexWriterConfig
				IndexWriterConfig iwConfig = new IndexWriterConfig(
						Version.LUCENE_45, analyzer);
				iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
				iwriter = new IndexWriter(dir, iwConfig);
				// 写入索引
				Document doc = new Document();
				Field f1 = new Field("ID", "1000", TextField.TYPE_STORED);
				Field f2 = new Field(fieldName, text, TextField.TYPE_STORED);
				doc.add(f1);
				doc.add(f2);
				iwriter.addDocument(doc);
				iwriter.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			

	
	}	
}
